<template>
	<view class="content">
		<view>
			<view class="tabs">
				<view :class="['tabs-item',index==0?'tabs-item-tag':'']" @click="chooseTab(0)">全部门店</view>
				<view :class="['tabs-item',index==1?'tabs-item-tag':'']" @click="chooseTab(1)">常去门店</view>
			</view>
			<view :class="[index==0?'item':'']" v-show="index==0">
				<view class="video-list">
					<view class="video-content" v-for="(item, ind) in mendian" :key="ind" @click="goDevice(item.id)">
						<view class="video-describe">
							<view class="video-tit">
								{{item.name}}
							</view>
							<view class="video-reader">
								<image src="/static/dingwei.png" style="width: 25rpx;height: 20rpx;"></image>
								<text
									style="height: 20rpx;line-height: 20rpx;margin-left: 5rpx;">{{item.addressLat}}</text>
							</view>
							<view style="font-size: 44rpx;color: #000000;flex: 1;position: relative;"
								@click="goDevice(item.id)">
								<text
									style="display: inline-block;width: 90rpx;height: 40rpx;line-height: 33rpx;font-size: 20rpx;color: #127202;border: 1rpx solid #000000;text-align: center;box-sizing: border-box;margin-top: 10rpx;border-radius: 12rpx;top: 5rpx;position: absolute;left: 0rpx;border-style: solid;border-width: 1rpx;border-color: #127202;">{{item.onlineStatus}}</text>
								<text
									style="display: inline-block;width: 110rpx;height: 40rpx;line-height: 33rpx;font-size: 20rpx;color: #ffffff;border: 1rpx solid #127202;text-align: center;box-sizing: border-box;margin-top: 10rpx;border-radius: 12rpx;background-color: #127202;position: absolute;top: 5rpx;left: 100rpx;">接受预定</text>
							</view>
						</view>
						<view style="position: relative;width: 200rpx;">
							<text style="font-size: 18rpx;left: 50rpx;position: absolute;top:10rpx ;color: #666;"
								v-if="item.distance==''">未获取到位置</text>
							<text style="font-size: 18rpx;left: 43rpx;position: absolute;top:10rpx ;color: #666;"
								v-if="item.distance!=''">距离{{item.distance}}km</text>
							<image src="/static/gps.png"
								style="width: 60rpx;height: 60rpx;position: absolute;left: 70rpx;top: 55rpx;"></image>
							<text
								style="color: #127202;position: absolute;left: 65rpx;top: 125rpx;font-size: 24rpx;">去看看</text>
						</view>
					</view>
					<view style="height: 100rpx;"></view>
				</view>
			</view>
			<view :class="[index==1?'item':'']" v-show="index==1">
				<view class="video-list">
					<view class="video-content" v-for="(item, ind) in changqu" :key="ind" @click="goDevice(item.id)">
						<view class="video-describe">
							<view class="video-tit">
								{{item.name}}
							</view>
							<view class="video-reader">
								<image src="/static/dingwei.png" style="width: 25rpx;height: 20rpx;"></image>
								<text
									style="height: 20rpx;line-height: 20rpx;margin-left: 5rpx;">{{item.addressLat}}</text>
							</view>
							<view style="font-size: 44rpx;color: #000000;flex: 1;position: relative;"
								@click="goDevice(item.id)">
								<text
									style="display: inline-block;width: 90rpx;height: 40rpx;line-height: 33rpx;font-size: 20rpx;color: #127202;border: 1rpx solid #000000;text-align: center;box-sizing: border-box;margin-top: 10rpx;border-radius: 12rpx;top: 5rpx;position: absolute;left: 0rpx;border-style: solid;border-width: 1rpx;border-color: #127202;">{{item.onlineStatus}}</text>
								<text
									style="display: inline-block;width: 110rpx;height: 40rpx;line-height: 33rpx;font-size: 20rpx;color: #ffffff;border: 1rpx solid #127202;text-align: center;box-sizing: border-box;margin-top: 10rpx;border-radius: 12rpx;background-color: #127202;position: absolute;top: 5rpx;left: 100rpx;">接受预定</text>
							</view>
						</view>
						<view style="position: relative;width: 200rpx;">
							<text style="font-size: 18rpx;left: 50rpx;position: absolute;top:10rpx ;color: #666;"
								v-if="item.distance==''">未获取到位置</text>
							<text style="font-size: 18rpx;left: 43rpx;position: absolute;top:10rpx ;color: #666;"
								v-if="item.distance!=''">距离{{item.distance}}km</text>
							<image src="/static/gps.png"
								style="width: 60rpx;height: 60rpx;position: absolute;left: 70rpx;top: 55rpx;"></image>
							<text
								style="color: #127202;position: absolute;left: 65rpx;top: 125rpx;font-size: 24rpx;">去看看</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<m-tabbar native></m-tabbar>
	</view>
</template>

<script>
	import Api from '@/common/api';
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		formatDate
	} from '@/common/date';
	export default {
		components: {

		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		data() {
			return {
				index: 0,
				mendian: [],
				changqu: [],
				type: ''
			}
		},
		onLoad(options) {
			if (typeof(options.type) != 'undefined') {
				console.log(options.type)
				this.type = options.type
				uni.showModal({
					title: '提示',
					content: '请选择需要充值的门店',
					success: (e) => {
						if (e.confirm) {}
					}
				})
			}
			this.getAllStore();
		},
		onShow() {
			// this.getLocation();
		},
		methods: {
			call(mobile) {
				if (typeof(mobile) == 'undefined' || mobile == '') {
			
				} else {
					uni.makePhoneCall({
						phoneNumber: mobile,
						success: function() {
							console.log('拨打电话成功');
						},
						fail() {
							console.log('打电话失败了');
						}
					})
				}
			},
			// 定位授权
			getLocation() {
				let that = this;
				// 1、判断手机定位服务【GPS】 是否授权
				uni.getSystemInfo({
					success(res) {
						console.log("判断手机定位服务是否授权:", res);
						let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
						let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
						if (locationEnabled == false || locationAuthorized == false) {
							//手机定位服务（GPS）未授权
							uni.showToast({
								title: "请打开手机GPS",
								icon: "none",
							});
						} else {
							//手机定位服务（GPS）已授权
							// 2、判断微信小程序是否授权位置信息
							// 微信小程序已授权位置信息
							uni.authorize({
								//授权请求窗口
								scope: "scope.userLocation", //授权的类型
								success: (res) => {
									that.getLocationInfo();
								},
								fail: (err) => {
									err = err["errMsg"];
									uni
										.showModal({
											content: "需要授权位置信息",
											confirmText: "确认授权",
										})
										.then((res) => {
											console.log(res);
											if (res[1]["confirm"]) {
												uni.openSetting({
													success: (res) => {
														if (res.authSetting[
																"scope.userLocation"
															]) {
															// 授权成功
															uni.showToast({
																title: "授权成功",
																icon: "none",
															});
															that.getLocationInfo();
														} else {
															// 未授权
															uni.showToast({
																title: "授权失败,请重新授权",
																icon: "none",
															});
															uni.showModal({
																title: "授权",
																content: "获取授权" +
																	authouName +
																	"失败,是否前往授权设置？",
																success: function(
																	result
																) {
																	if (result
																		.confirm
																	) {
																		uni
																			.openSetting();
																	}
																},
																fail: function() {
																	uni.showToast({
																		title: "系统错误！",
																		icon: "none",
																	});
																},
															});
														}
													},
												});
											}
											if (res[1]["cancel"]) {
												// 取消授权
												uni.showToast({
													title: "你拒绝了授权，无法获得周边信息",
													icon: "none",
												});
											}
										});
								},
								complete(res) {
									// console.log('授权弹框', res);
									if (res.errMsg == "authorize:ok") {
										that.getLocationInfo();
									} else {
										uni.showModal({
											title: "授权",
											content: "获取授权" + authouName + "失败,是否前往授权设置？",
											success: function(result) {
												if (result.confirm) {
													uni.openSetting();
												}
											},
											fail: function() {
												uni.showToast({
													title: "系统错误！",
													icon: "none",
												});
											},
										});
									}
								},
							});
						}
					},
				});
			},
			getLocationInfo() {
				let that = this;
				uni.getLocation({
					type: 'wgs84',
					success(res) {
						console.log(res);
						uni.setStorageSync('longitude', res.longitude);
						uni.setStorageSync('latitude', res.latitude);
						that.getAllStore();
					},
					fail: function(res) {
						console.log(res);
					}
				});
			},
			chooseTab(index) {
				this.index = index;
				if (index == 0) {
					this.getAllStore();
				} else {
					this.getChangqu();
				}
			},
			async getAllStore() {
				let params = {
					longitude: uni.getStorageSync('longitude'),
					latitude: uni.getStorageSync('latitude')
				}
				console.log(params)
				if (typeof uni.getStorageSync('longitude') == 'undefined') {
					params = {}
				}
				let result = await Api.apiCall('get', Api.qsg.getAllStore, params);
				if (result) {
					console.log(result)
					this.mendian = result;
				}
			},
			async getChangqu() {
				let params = {
					longitude: uni.getStorageSync('longitude'),
					latitude: uni.getStorageSync('latitude')
				}
				if (typeof uni.getStorageSync('longitude') == 'undefined') {
					params = {}
				}
				let result = await Api.apiCall('get', Api.qsg.bestStore, params);
				if (result) {
					console.log(result)
					this.changqu = result;
				}
			},
			goDevice(id) {
				console.log(this.type)
				if (this.type == 'chongzhi') {
					uni.navigateTo({
						url: '/pages/money/recharge?storeId=' + id
					})
				} else {
					uni.navigateTo({
						url: '/pages/meallist/selectRoom?storeId=' + id
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.priceSign {
		font-size: 24rpx;
	}

	.priceNum {
		font-size: 32rpx;
		font-weight: bold;
	}

	.priceIcon {
		font-size: 24rpx;
	}

	.schoolName {
		width: 750rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 50rpx;
		font-weight: bold;
		color: #000000;
	}

	.uni-input {
		padding: 0 24rpx;
	}

	.choese {
		width: 750rpx;
		display: flex;
		box-sizing: border-box;
		padding: 0 30rpx;
		color: #888888;
	}

	.dataName {
		height: 60rpx;
	}

	.currentSchool {
		height: 60rpx;
		background: #FFFFFF;
	}

	.video-tit {
		font-size: 30rpx;
		color: #000000;
		font-weight: bolder;
		display: inline-block;
		vertical-align: top;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.checkMore text {
		display: block;
		width: 100%;
		height: 60rpx;
		line-height: 58rpx;
		font-size: 24rpx;
		color: #172a89;
		border: 1px solid #172A89;
		text-align: center;
		box-sizing: border-box;
		margin-top: 46rpx;
		border-radius: 50rpx;
	}

	.checkMore {
		font-size: 24rpx;
		color: #172a89;
		flex: 1;
	}

	.video-price {
		font-size: 30rpx;
		color: #666;
		overflow: hidden;
		display: inline-block;
		vertical-align: top;
		text-align: center;
		box-sizing: border-box;
	}

	.video-reader {
		font-size: 22rpx;
		font-weight: 500;
		color: #666;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		-o-text-overflow: ellipsis;
	}

	.video-describe {
		display: inline-block;
		vertical-align: top;
		width: 480rpx;
		border-right-style: solid;
		border-width: 3rpx;
		border-color: #f7f7f7
	}

	.video-cover {
		width: 100rpx;
		height: 100rpx;
		margin: 30rpx 30rpx 30rpx 0;
		display: inline-block;
		vertical-align: top;
		border-radius: 50%;
		flex: 1;
	}

	.video-cover image {
		width: 100rpx;
		height: 100rpx;
		background: #FFFFFF;
		border-radius: 50%;
	}

	.video-content {
		width: 690rpx;
		height: 210rpx;
		padding: 15rpx 0;
		border-bottom: 25rpx solid #f7f7f7;
		box-sizing: border-box;
		display: flex;
		background-color: #FFFFFF;
		padding-left: 22rpx;
		border-radius: 20rpx;
	}
	.video-list {
		width: 750rpx;
		height: auto;
		padding: 0 30rpx;
		margin-top: 15rpx;
		box-sizing: border-box;
	}

	.content {
		.tabs {
			height: 60rpx;
			display: flex;
			margin-top: 10rpx;

			.tabs-item {
				// 平均分布
				flex: 1;
				text-align: center;
				color: #111111;
				height: 60rpx;
				line-height: 60rpx;
			}

			.tabs-item-tag {
				color: #127202;
				border-bottom: 4rpx solid #127202;
			}
		}

		.item {
			padding: 10rpx;
			height: 2000rpx;
			background-color: #f7f7f7;
			display: grid;

			image {
				width: 400rpx;
				margin: auto;
			}

			text {
				text-align: center;
			}
		}
	}
</style>
